
#ifndef __MINILIB_H__
#define __MINILIB_H__

////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// zc_stack_t.
struct zc_stack_t
{
    int size;
    int length;
    void **data;
};

struct zc_stack_t *zc_stack_create(int size);
int zc_stack_realloc(struct zc_stack_t *s, int size);
int zc_stack_push(struct zc_stack_t *s, void *d);
void *zc_stack_pop(struct zc_stack_t *s);
int zc_stack_length(struct zc_stack_t *s);
int zc_stack_isfull(struct zc_stack_t *s);
void zc_stack_clear(struct zc_stack_t *s);
void zc_stack_destory(struct zc_stack_t *s);

////////////////////////////////////////

// zc_queue
struct zc_queue_t
{
    int size;
    int length;

    int head;
    int tail;

    void **data;
};

struct zc_queue_t *zc_queue_create(int size);
int zc_queue_realloc(struct zc_queue_t *q, int size);
int zc_queue_add(struct zc_queue_t *q, void *d);
void *zc_queue_del(struct zc_queue_t *q);
int zc_queue_length(struct zc_queue_t *q);
int zc_queue_isfull(struct zc_queue_t *q);
void zc_queue_clear(struct zc_queue_t *q);
void zc_queue_destory(struct zc_queue_t *q);

////////////////////////////////////////

// singal list node
struct zc_slnode_t
{
    void *data;
    struct zc_slnode_t *next;
};

// signal list.
struct zc_slist_t
{
    int length;
    struct zc_slnode_t *head, *tail;
};

// pointor for special function.
typedef int (*zc_slist_func_t)(void *data, void *arg);

int zc_slist_init(struct zc_slist_t *sl);
int zc_slist_append(struct zc_slist_t *sl, void *d);
int zc_slist_insert(struct zc_slist_t *sl, void *d1, void *d2);
int zc_slist_delete(struct zc_slist_t *sl, void *d);
int zc_slist_length(struct zc_slist_t *sl);
struct zc_slnode_t *zc_slist_find(struct zc_slist_t *sl, void *d);
int zc_slist_foreach(struct zc_slist_t *sl, zc_slist_func_t func, void *arg);
int zc_slist_clear(struct zc_slist_t *sl);

////////////////////////////////////////

// double list node.
struct zc_dlnode_t
{
    void *data;
    struct zc_dlnode_t *prov, *next;
};

// double list.
struct zc_dlist_t
{
    int length;
    struct zc_dlnode_t *head, *tail;
};

typedef void (*zc_dlist_func_t)(void *data, void *arg);

int zc_dlist_init(struct zc_dlist_t *dl);
int zc_dlist_append(struct zc_dlist_t *dl, void *d);
int zc_dlist_insert(struct zc_dlist_t *dl, void *d1, void *d2);
int zc_dlist_delete(struct zc_dlist_t *dl, void *d);
int zc_dlist_length(struct zc_dlist_t *dl);
struct zc_dlnode_t *zc_dlist_find(struct zc_dlist_t *dl, void *d);
void zc_dlist_foreach(struct zc_dlist_t *dl, zc_dlist_func_t func, void *a);
int zc_dlist_clear(struct zc_dlist_t *dl);
void *zc_dlist_pop(struct zc_dlist_t *dl);
int zc_dlist_push(struct zc_dlist_t *dl, void *d);

////////////////////////////////////////

// btree

typedef int (*zc_btree_cmp_t)(void *data1, void *data2);
typedef void (*zc_btree_func_t)(void *data, void *arg);

// zc_btree node
struct zc_btnode_t
{
    void *data;
    struct zc_btnode_t *left, *right;
};

// zc_btree
struct zc_btree_t
{
    int length;
    struct zc_btnode_t *root;
    zc_btree_cmp_t cmp;
};

int zc_btree_init(struct zc_btree_t *bt, zc_btree_cmp_t cmp);
struct zc_btnode_t *zc_btree_add(struct zc_btree_t *bt, void *d);
int zc_btree_delete(struct zc_btree_t *bt, void *d);
struct zc_btnode_t *zc_btree_find(struct zc_btree_t *bt, void *d);
void zc_btree_foreach(struct zc_btnode_t *btn, zc_btree_func_t func, void *arg);
void zc_btree_destory(struct zc_btnode_t *btn);

////////////////////////////////////////

// memory pool

struct zc_mempool_node_t
{
	struct zc_mempool_node_t *next;
	char *data;
};

struct zc_mempool_t
{
	int size;
	int count;

	struct zc_mempool_node_t *use_point;
	int used;
	struct zc_mempool_node_t *free_point;
	int freed;

	struct zc_mempool_node_t *data;
};

struct zc_mempool_t *zc_mempool_new(int size);
char *zc_mempool_get(struct zc_mempool_t *m);
int zc_mempool_put(struct zc_mempool_t *m, char *d);
int zc_mempool_destroy(struct zc_mempool_t **m);
int zc_mempool_stat(struct zc_mempool_t *m);

////////////////////////////////////////

// hash node
struct zc_hash_node_t
{
	int key;
	char *data;
	struct zc_hash_node_t *node_ex;
	int count;
	int size;
};

// hash struct
struct zc_hash_t
{
	struct zc_hash_node_t *node_array;
	int data_size;
	int count;
	int size;
};

struct zc_hash_t *zc_hash_new(int hash_size, int data_size);
int zc_hash_add(struct zc_hash_t *h, int key, char *data);
int zc_hash_del(struct zc_hash_t *h, int key);
char *zc_hash_find(struct zc_hash_t *h, int key);
void zc_hash_destroy(struct zc_hash_t **hh);
void zc_hash_stat(struct zc_hash_t *h);

////////////////////////////////////////

#endif // __MINILIB_H__
